use axum::{
    extract::{Json as ExtractJson, State},
    http::StatusCode,
    response::IntoResponse,
};
use std::sync::Arc;
use tracing::debug;

use crate::AppState;

pub(crate) async fn create_account(State(state): State<Arc<AppState>>) -> impl IntoResponse {
    debug!("create account");
    let mng = state.bc_manager.lock().await;
    let r = mng.create_keypair().await;
    match r {
        Err(e) => {
            let response = serde_json::json!({"error": e.to_string()});
            (StatusCode::INTERNAL_SERVER_ERROR, ExtractJson(response))
        }
        Ok(pub_key_hash) => {
            debug!("pub_key_hash: {}", pub_key_hash.to_hex_str());
            let response = serde_json::json!({
                "pub_key_hash": pub_key_hash
            });

            (StatusCode::CREATED, ExtractJson(response))
        }
    }
}
